package sections;

import static objects.AdapletConstants.sNewLine;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import objects.IObject;
import objects.Interface;

public class InterfaceSection implements ISection
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1563946902511153004L;
	private Map<String, Interface> fInterfaceList;

	public InterfaceSection()
	{
		fInterfaceList = new LinkedHashMap<String, Interface>();
	}

	public Interface createNewInterface(String interfaceName)
	{
		Interface newInterface = new Interface();

		newInterface.setInterfaceName(interfaceName);
		fInterfaceList.put(interfaceName, newInterface);

		return newInterface;
	}

	public Interface getInterface(String interfaceName)
	{
		return fInterfaceList.get(interfaceName);
	}

	public String buildHeaderComment()
	{
		StringBuffer commentBuf = new StringBuffer(300);

		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("################################################################################");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("# Mapping of physical Matrix Interfaces to foreign data tables.  The");
		commentBuf.append(sNewLine);
		commentBuf.append("# following keywords are avalable:");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("#   baseobject xx in id TABLE(COLUMN)");
		commentBuf.append(sNewLine);
		commentBuf.append("#   next EXPRESSION;");
		commentBuf.append(sNewLine);
		commentBuf.append("#   attribute name TABLE(COLUMN) [using TABLE(COLUMN)]");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("################################################################################");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);

		return commentBuf.toString();
	}

	public String buildInterfaceSectionDefinition()
	{
		StringBuffer sectionBuf = new StringBuffer(fInterfaceList.size() * 500);

		sectionBuf.append(buildHeaderComment());
		sectionBuf.append(sNewLine);

		for (String interfaceName : fInterfaceList.keySet())
		{
			Interface lInterface = fInterfaceList.get(interfaceName);
			sectionBuf.append(lInterface.toString());
			sectionBuf.append(sNewLine);
		}

		return sectionBuf.toString();
	}

	@Override
	public String toString()
	{
		return buildInterfaceSectionDefinition();
	}

	@Override
	public int getCount()
	{
		return fInterfaceList.size();
	}

	@Override
	public Section getSectionType()
	{
		return Section.INTERFACE;
	}

	@Override
	public IObject[] getObjects()
	{
		return fInterfaceList.values().toArray(new Interface[0]);
	}

	private void readObject(ObjectInputStream iStream) throws IOException, ClassNotFoundException
	{
		iStream.defaultReadObject();
		validateState();
	}

	private void validateState()
	{
		// TODO Auto-generated method stub

	}

	private void writeObject(ObjectOutputStream oStream) throws IOException
	{
		oStream.defaultWriteObject();
	}

	public void removeInterface(Interface iObject)
	{
		fInterfaceList.remove(iObject.getObjectName());
	}
}
